<template>
    <div class="app-container content-wrapper">
        <div class="filter-container">
            <el-form :model="listQuery" :inline="true" ref="filterForm" style="display: inline-block">
                <el-form-item prop="account" class="filter-item">
                    <el-input v-model="listQuery.account" placeholder="账号" style="width: 150px;"></el-input>
                </el-form-item>
                <el-form-item prop="realName" class="filter-item">
                    <el-input v-model="listQuery.realName" placeholder="姓名" style="width: 150px;"></el-input>
                </el-form-item>
                <el-form-item prop="sex" class="filter-item">
                    <el-select v-model="listQuery.sex" placeholder="性别" clearable style="width: 100px">
                        <el-option
                                v-for="item in sexOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="status" class="filter-item">
                    <el-select v-model="listQuery.status" placeholder="状态" style="width: 100px">
                        <el-option
                                v-for="item in statusOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="orderBy" class="filter-item">
                    <el-select v-model="listQuery.orderBy" placeholder="排序" style="width: 150px">
                        <el-option
                                v-for="item in sortOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item" type="primary" icon="mars-reset" @click="handleResetFilter">重置</el-button>
            </el-form>
        </div>

        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">

            <el-table-column label="序号" width="65" align="center">
                <template slot-scope="scope"> {{scope.row.id}} </template>
            </el-table-column>

            <el-table-column label="姓名" width="110px" align="center">
                <template slot-scope="scope"> {{scope.row.users[0].realName}} </template>
            </el-table-column>

            <el-table-column label="性别" width="80px" align="center">
                <template slot-scope="scope"> {{scope.row.users[0].sex | sexFilter}} </template>
            </el-table-column>

            <el-table-column label="账号" width="110px" align="center">
                <template slot-scope="scope"> {{scope.row.users[0].account}} </template>
            </el-table-column>

            <el-table-column label="手机号码" width="130px" align="center">
                <template slot-scope="scope"> {{scope.row.users[0].mobile}} </template>
            </el-table-column>

            <el-table-column label="邮件地址" min-width="200px" align="center" show-overflow-tooltip>
                <template slot-scope="scope"> {{scope.row.users[0].email}} </template>
            </el-table-column>

            <el-table-column label="状态" align="center">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.users[0].status | statusTagFilter">{{scope.row.users[0].status | statusFilter}}</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="最后修改时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.users[0].utime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

            <el-table-column label="创建时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.users[0].ctime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

            <el-table-column label="操作" width="120px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="danger" icon="delete" @click="handleDelete(scope.row)">移除</el-button>
                </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
    import fetch from '@/utils/fetch'
    import { fetchGroupUserList, removeGroupUser } from '@/api/upms/group'

    const sexOptions = [{label: '男', key: 1}, {label: '女', key: 2}, {label: '未知', key: 3}]
    const sortOptions = [{label: '按ID升序', key: 'id asc'}, {label: '按ID降序', key: 'id desc'}]
    const statusOptions = [{label: '正常', key: 1}, {label: '锁定', key: 2}]

    export default {
        name: 'user',
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    rows: 10,
                    groupId: this.$route.params.groupId,
                    account: undefined,
                    realName: undefined,
                    status: undefined,
                    sex: undefined,
                    orderBy: 'id asc'
                },
                sexOptions,
                statusOptions,
                sortOptions
            }
        },
        created() {
            this.getList()
        },
        filters: {
            sexFilter(sex) {
                const sexArray = ['男', '女', '未知']
                return sexArray[sex?--sex:'']
            },
            statusFilter(status) {
                const statusArray = ['正常', '锁定']
                return statusArray[status?--status:'']
            },
            statusTagFilter(status) {
                const statusArray = ['success', 'danger']
                return statusArray[status?--status:'']
            }
        },
        methods: {
            getList() {
                this.listLoading = true
                fetchGroupUserList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch((error)=>{
                    console.log(error)
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleCloseDialog() {
                this.resetForm()
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            handleResetFilter() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleDelete(row) {
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    removeGroupUser({groupAssingId: row.id}).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getList()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            resetForm() {
                this.$refs['form'].resetFields()
                this.form = {
                    name: '',
                    identity: '',
                    remarks: ''
                }
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>